<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">登 &nbsp; 录 &nbsp; 界 &nbsp; 面</h3>
      </div>

      <el-form-item prop="userName">
        <span class="svg-container">
          <i class="el-icon-user-solid"></i>
        </span>
        <el-input
          ref="userName"
          v-model="loginForm.userName"
          placeholder="用户名"
          name="userName"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip
        v-model="capsTooltip"
        content="大写开启"
        placement="right"
        manual
      >
        <el-form-item prop="passWord">
          <span class="svg-container">
            <i class="el-icon-lock"></i>
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.passWord"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span
            class="show-pwd"
            @click="showPwd"
          >
            <i class="el-icon-view"></i>
          </span>
<!--          获取验证码以及更新验证码-->
          <el-form-item prop="verifyCode">
            <span class="svg-container">
              <i class="el-icon-message"></i>
            </span>
            <el-input
              ref="verifyCode"
              v-model="loginForm.verifyCode"
              placeholder="验证码"
              name="verifyCode"
              type="text"
              tabindex="3"
              autocomplete="on"
              @keyup.enter.native="handleLogin"
            />
            <span
              class="show-verify-code"
              @click="refreshVerifyCode"
            >
              <i class="el-icon-view"></i>
            </span>
          </el-form-item>
        </el-form-item>
      </el-tooltip>
      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click="handleLogin"
      >登录</el-button>

    </el-form>

  </div>
</template>

<script>
import { commonAPI } from '../../api/commonAPI'
import layout from '../layout/Layout'
import crypto from 'crypto'

export default {
  components: { layout },
  data () {
    return {
      loginForm: {
        userName: '',
        password: '',
        passWord: '',
        verifyCode: '',
        verificationCode: ''
      },
      capsTooltip: false,
      passwordType: 'password',
      loading: false,
      loginRules: {
        userName: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写用户名'
          }
        ],
        passWord: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写密码'
          },
          { type: 'string', min: 6, message: '密码至少为6位', trigger: 'blur' }
        ],
        verifyCode: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写验证码'
          }
        ]
      }
    }
  },
  methods: {
    checkCapslock ({ shiftKey, key } = {}) {
      console.log('shiftKey', shiftKey)
      console.log('key', key)
      if (key && key.length === 1) {
        if (
          (shiftKey && (key >= 'a' && key <= 'z')) ||
            (!shiftKey && (key >= 'A' && key <= 'Z'))
        ) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd (e) {
      console.log(e)
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      const el = e.target
      this.passwordType === ''
        ? el.setAttribute('style', 'color: #409EFF')
        : el.setAttribute('style', 'color: #c0c4cc')
    },
    handleLogin: function () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          let md5 = crypto.createHash('md5')
          md5.update(this.loginForm.passWord) //  需要加密的密码
          this.loginForm.password = md5.digest('hex') //  password 加密完的密码
          console.log(this.loginForm.password)
          // 为了看到登录转圈圈的加载效果，这里来一个延迟2秒发送请求
          // 封装的公共请求API
          commonAPI('queryUser', this.loginForm)
            .then(res => {
              let data = res.data
              console.log(data)
              if (data.info.code === '0' && data.data.status === 'OK') {
                this.$store.dispatch('changeUserName', this.loginForm.userName)
                this.$store.dispatch('saveToken', data.data.token)
                setTimeout(() => {
                  this.$router.push({
                    name: 'layoutYHGL'
                  })
                }, 2000)
              } else {
                this.$message({
                  message: '用户名或密码错误',
                  type: 'warning'
                })
                this.loading = false
              }
            })
            .catch(error => {
              this.loading = false
              this.$notify({
                // 这里采用element ui的一个错误显示效果模板
                title: '登录提示',
                message: error.message,
                position: 'bottom-right',
                type: 'error'
              })
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    refreshVerifyCode () {
      this.verifyCode = ''
      this.verifyCode = this.getVerifyCode()
    }
  }
}
</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg: #283443;
  $light_gray: #fff;
  $cursor: #fff;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;

  .login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;

    .login-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding: 160px 35px 0;
      margin: 0 auto;
      overflow: hidden;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 5px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }
    .svg-container-code {
      padding: 5px 0px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 15px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 16px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .code {
      position: absolute;
      right: 0px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .thirdparty-button {
      position: absolute;
      right: 0;
      bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
      .thirdparty-button {
        display: none;
      }
    }
  }
</style>
